// pages/registerbox/register2/register2.js
import {
  subRegister,
  getCode
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    registerInfo: null,
    downtime:60,
    vcode:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let registerInfo = JSON.parse(options.registerInfo);
    this.setData({
      registerInfo: registerInfo
    })

  },
  //获取验证码
  getvcode:function(e){
    this.setData({
      vcode: e.detail.value
    }) 
  },
  //获取发件人名字
  getName: function(e) {
    this.setData({
      ['registerInfo.name']: e.detail.value
    })
  },
  //获取发件人电话
  getPhone: function(e) {
    this.setData({
      ['registerInfo.phone']: e.detail.value
    })
  },
  //提交
  onSubmit: function() {
    let {
      registerInfo,
      vcode
    } = this.data;
    if (registerInfo.name == "") {
      errorMsg("请输入发件人姓名")
    } else if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(registerInfo.phone)) {
      errorMsg("发件人手机号不正确")
    } else {
      subRegister("", "", registerInfo.name, registerInfo.teamnum, registerInfo.ordernum, registerInfo.leibie, registerInfo.phone, vcode, registerInfo.sendaddress,"register2", (r) => {
        if (r.code == 0) {
          wx.reLaunch({
            url: '../../about/about',
          })
          setTimeout(function(){
            errorMsg("注册成功");
          },400)
          
        } else {
          errorMsg(r.message);
        }
      })
    }

  },
  //获取验证码
  getCode: function() {
    let self = this;
    let { registerInfo } = this.data;
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(registerInfo.phone)) {
      errorMsg('手机号不正确');
    } else {
      if (this.data.downtime == 60) {
        getCode(registerInfo.phone,"register2", (r) => {

          if (r.code == 0) {
            this.setData({
              downtime: --self.data.downtime
            })
            let siv = setInterval(() => {
              if (self.data.downtime > 1) {
                self.setData({
                  downtime: --self.data.downtime
                })
              } else {
                clearInterval(siv);
                self.setData({
                  downtime: 60
                })
              }

            }, 1000)
          } else {
            errorMsg(r.message);
          }
        })
      }
    }

  }

})